package hoowin.basic.action;

import hoowin.app.wsnd.service.WsndUserService;
import hoowin.basic.model.Account;
import hoowin.basic.model.User;
import hoowin.basic.service.AccountService;
import hoowin.basic.service.LogService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class CreateAccount extends ActionSupport {
	private int phase;
	private int invitor;
	private String email;
	private String code;
	private String name;
	private String password;
	private String sex;
	private String language;
	private String message;
	private String nationality;
	private int month;
	private int day;
	private int year;
	private String words;
	
	private AccountService accountService;
	private WsndUserService wsndUserService;
	private LogService logService;
	private User user;
	
	public String execute(){
		switch(phase){
		case 1:{
			email = accountService.decodeToEmail(code);
			if(email == null){
				setMessage(getText("error.1005"));
				return ERROR;
			}
			invitor = accountService.getInvitorByEmail(email);
			if(invitor==0){
				setMessage(getText("error.1006"));
				return ERROR;
			}
			phase++;
			return SUCCESS;
		}case 2:{
			boolean vali = true;
			if(!checkName(name)){
				this.addFieldError("name", getText("message.1003"));
				vali = false;
			}if(!checkPassword(password)){
				this.addFieldError("password", getText("message.1004"));
				vali = false;
			}if(!(sex.equals("m") || sex.equals("f"))){
				this.addFieldError("sex", getText("message.1005"));
				vali = false;
			}if(!(language.equals("en") || language.equals("zh"))){
				this.addFieldError("language", getText("message.1006"));
				vali = false;
			}if(vali){
				Account acc = new Account();
				acc.setEmail(email);
				acc.setPassword(password);
				acc.setPhone("00000000000");
				String info = accountService.createAccount(acc, name, language, sex, invitor);
				if(info.equals(SUCCESS)){
					info = logService.doLogin(email, password);
					if(info.equals(SUCCESS)){
						phase++;
						return SUCCESS;
					}else{
						setMessage(getText(info));
						return ERROR;
					}
				}
				setMessage(getText(info));
				return ERROR;
			}else return INPUT;
			
		}case 3:{
			if(month>0&&month<13&&day>0&&day<32&&year>1900&&year<2012){
				if(nationality==null || nationality.length()==0){
					this.addFieldError("nationality", getText("message.1005"));
					return INPUT;
				}else if(words==null || words.length()==0){
					this.addFieldError("words", getText("message.1006"));
					return INPUT;
				}else{
					String info = wsndUserService.addWsndUser(user.getId(),nationality, month, day, year);
					if(info.equals(SUCCESS)){
						user.getUp().updateUserWords(words, user.getId());
						phase++;
						return SUCCESS;
					}else{
						setMessage(getText(info));
						return ERROR;
					}
					
				}
			}else{
				this.addFieldError("birthday", getText("message.1004"));
				return INPUT;
			}
		}default:{
			setMessage(getText("message.1004"));
			return ERROR;
		}
		}
	}
	
	public void setPhase(int phase) {
		this.phase = phase;
	}
	public int getPhase() {
		return phase;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return email;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getCode() {
		return code;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return password;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getSex() {
		return sex;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getLanguage() {
		return language;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setInvitor(int invitor) {
		this.invitor = invitor;
	}

	public int getInvitor() {
		return invitor;
	}
	private boolean checkName(String n){
		if(n==null || n.length()==0)return false;
		if(n.length()>25||n.length()<2)return false;
		return true;
	}
	private boolean checkPassword(String pass){
		if(pass==null || pass.length()==0)return false;
		if(pass.length()<6)return false;
		return true;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setNationality(String nationality) {
		this.nationality = nationality;
	}

	public String getNationality() {
		return nationality;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getMonth() {
		return month;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getDay() {
		return day;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getYear() {
		return year;
	}

	public void setWords(String words) {
		this.words = words;
	}

	public String getWords() {
		return words;
	}

	public void setWsndUserService(WsndUserService wsndUserService) {
		this.wsndUserService = wsndUserService;
	}

	public WsndUserService getWsndUserService() {
		return wsndUserService;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}
}
